package com.qdc.datacenters.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qdc.datacenters.client.CorpCustomerClient;
import com.qdc.datacenters.domain.entity.CorpCustomer;
import com.qdc.datacenters.domain.query.chatmsg.ChatMsgListQuery;
import com.qdc.datacenters.domain.query.qw.QwCustomerQuery;
import com.qdc.datacenters.domain.vo.chatmsg.ChatMsgCustomerListVo;
import com.qdc.datacenters.domain.vo.qw.QwCustomerDetailVo;
import com.qdc.datacenters.mapper.CorpCustomerMapper;
import com.qdc.datacenters.service.ICorpCustomerService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 企业客户信息(CorpCustomer)表服务实现类
 *
 * @author danmo
 * @since 2024-03-04 15:44:05
 */
@Slf4j
@Service
public class CorpCustomerServiceImpl extends BaseServiceImpl<CorpCustomerMapper, CorpCustomer> implements ICorpCustomerService {

    @Resource
    private CorpCustomerClient corpCustomerClient;

    private final ReentrantLock lock = new ReentrantLock();

    @Override
    public CorpCustomer getByExternalUserId(String externalUserId) {
        CorpCustomer corpCustomer = null;

        try {
            if (!lock.tryLock(3, TimeUnit.SECONDS)) {
                return corpCustomer;
            }
        } catch (InterruptedException e) {
            log.error("get customer error msg:{}",e.getMessage(),e);
            return corpCustomer;
        }
        try {
            corpCustomer = getOne(new LambdaQueryWrapper<CorpCustomer>()
                    .eq(CorpCustomer::getExternalUserId, externalUserId)
                    .last("limit 1"));
            if (Objects.isNull(corpCustomer)) {
                QwCustomerQuery query = new QwCustomerQuery();
                query.setExternal_userid(externalUserId);
                QwCustomerDetailVo customerDetail = corpCustomerClient.getCustomerDetail(query);
                if (Objects.nonNull(customerDetail) && Objects.equals(0, customerDetail.getErrCode())) {
                    CorpCustomer addCustomer = getCorpCustomer(customerDetail);
                    if (save(addCustomer)) {
                        corpCustomer = addCustomer;
                    }
                }
            }
        } finally {
            lock.unlock();
        }
        return corpCustomer;
    }

    @Override
    public List<ChatMsgCustomerListVo> getCustomerMsgList(ChatMsgListQuery query) {
        return this.baseMapper.getCustomerMsgList(query);
    }


    @NotNull
    private static CorpCustomer getCorpCustomer(QwCustomerDetailVo customerDetail) {
        QwCustomerDetailVo.ExternalContact externalContact = customerDetail.getExternalContact();
        CorpCustomer addCustomer = new CorpCustomer();
        addCustomer.setExternalUserId(externalContact.getExternalUserId());
        addCustomer.setAvatar(externalContact.getAvatar());
        addCustomer.setGender(externalContact.getGender());
        addCustomer.setName(externalContact.getName());
        addCustomer.setType(externalContact.getType());
        addCustomer.setUnionid(externalContact.getUnionId());
        addCustomer.setCorpName(externalContact.getCorpName());
        addCustomer.setCorpFullName(externalContact.getCorpFullName());
        addCustomer.setPosition(externalContact.getPosition());
        return addCustomer;
    }
}

